<?php

// 4/11/2010: immanuel192 write this action to show verse edit page

if (!defined('EXPONENT')) exit('');
global $db;

$song = null; $song_id=-1;
if (isset($_REQUEST['id'])) {
	$_REQUEST['id']=intval($_REQUEST['id']); $song_id = $_REQUEST['id'];
	// get song and collection object
	$sql="SELECT {$db->prefix}present_songs.*, {$db->prefix}present_song_collection.name collection_name, {$db->prefix}present_song_collection.id collection_id
		  FROM {$db->prefix}present_songs INNER JOIN {$db->prefix}present_song_collection
			   ON {$db->prefix}present_songs.collection_id = {$db->prefix}present_song_collection.id
		  WHERE {$db->prefix}present_songs.id = {$song_id}
		  LIMIT 0,1";
	$song = $db->selectObjectsBySql($sql);
	// get first object
	if ($song)
		$song=$song[0];
}

if ($song_id==-1)
	validator::failAndReturnToForm('Wrong song  value.', $_POST);

$verse_edit_mode = 0;

if (($song && exponent_permissions_check('edit',$loc)) || (!$song && exponent_permissions_check('create',$loc))) {
	// get all song verse
	$verse = $db->selectObjects("present_song_verse","song_id = " . $song_id . " ORDER BY rank");
	foreach ($verse as $key=>$value)
	{
		// for background
		if ($value->background <= 0) {
			$value->picpath = '';
		} else {
			$file = $db->selectObject('file', 'id='.$value->background);
			$value->picpath = $file->directory.'/'.$file->filename;
		}
		// clear html format in-order to be shown well
		$value->content=strip_tags($value->content);
		$verse[$key]=$value;
	}
	// search for verse we want to edit
	$verse_id = -1; $edit_verse=null;
	if (isset($_REQUEST['verse_id']))
	{
		$_REQUEST['verse_id'] = intval($_REQUEST['verse_id']);
		$verse_id=$_REQUEST['verse_id'];
		foreach ($verse as $key=>$value)
		{
			// get verse item to edit
			if ($value->id == $verse_id)
			{
				$edit_verse=$value; $verse_edit_mode = 1;
				break;
			}
		}
	}
	
	$form = slide_song_verse::form($song,$edit_verse);
	$form->location($loc);
	$form->meta('action','save_verse');
	$form->meta('module','slidemodule');
	
	$template = new template('slidemodule','_form_verse_edit');
	$template->assign('is_edit',(isset($link->id)? 1 : 0));
	$template->assign('song',$song);
	$template->assign('verse',$verse);
	$template->assign('verse_edit_mode',$verse_edit_mode);
	$template->assign('form_html',$form->toHTML());
	$template->register_permissions(slidemodule::permissions("",1),$loc);
	if ($verse_edit_mode == 0)
		exponent_flow_set(SYS_FLOW_PROTECTED,SYS_FLOW_ACTION);
	$template->output();
} else {
	echo SITE_404_HTML;
}

?>
